const Base = require('./base.js');

module.exports = class extends Base {
  
  tokenSerivce = this.service('token')
  async __before() {
  }
  // 创建超级管理员，
  async createspadminAction() {
    const res = await this.auth.createSuperAdmin(this.ctx)
    return this.success(res, '创建成功')
  }

  // 登录
  async loginAction() {
    if(this.isPost) {
      try {
        const res = await this.auth.checkUser(this.ctx)
        if(!res.length) {
          return this.fail(401, '账号或密码错误')
        }
        const { username, role_id, r_name } = res[0]
        const token = await this.tokenSerivce.sign({ username, role_id })
        return this.success({ username, role_id, r_name, token }, '请求成功')
      } catch(err) {
        return this.fail(500, err)
      }
    }
    return this.fail(500, '无效的方法')
  }

  // 获取用户信息                                                                                                                      
  async infoAction() {
    this.tokenSerivce.verify(this.get('token'))
  }
  
};
